define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'booking/comment/index' + location.search,
                    add_url: 'booking/comment/add',
                    edit_url: 'booking/comment/edit',
                    del_url: 'booking/comment/del',
                    multi_url: 'booking/comment/multi',
                    import_url: 'booking/comment/import',
                    table: 'booking_comment',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'pid', title: __('Pid'), formatter: Table.api.formatter.search},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'source_id', title: __('Source_id'), formatter: Table.api.formatter.search},
                        {field: 'user_id', title: __('User_id'), formatter: Table.api.formatter.search},
                        {field: 'user.nickname', title: __('User'),operate:false},
                        {field: 'comment', title: __('Comment'), operate: 'LIKE',formatter: Controller.api.content},
                        {field: 'grade', title: __('Grade'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'),searchList: {"normal": __('Status normal'), "hidden": __('Status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[ {
                            name: '回复',
                            title: function(row){
                                return '回复（'+row.user.nickname+'）';
                            },
                            classname: 'btn btn-xs btn-primary btn-dialog',
                            text:function(row){
                                return '回复（'+row.comments+'）';
                            },
                            icon: 'fa fa-comment',
                            url: 'booking/comment/reply/pid/{id}',
                            callback: function (data) {}
                        }]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        reply: function () {
            let refresh = function(){
                window.location.reload();
                parent.$("#table").bootstrapTable('refresh', {});
            }
            $(document).on('click','.btn-reply',function(){
                Form.api.submit($("form[role=form]"));
                setTimeout(refresh,1500)
            })
            $('.btn-delone').data('success',refresh)
            $('.btn-dialog').data('end',refresh)
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            content: function (value, row, index) {
                var width = this.width != undefined ? (this.width.match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
                return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";' title='"+value+"' data-toggle='tooltip' data-placement='right'>" + value + "</div>";
            },
        }
    };
    return Controller;
});
